package com.example.yichuangyun.mvvm.ui.activity.view;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.http.ApiFactory;
import com.example.yichuangyun.model.contacts.ContactsTagModel;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.example.yichuangyun.utils.ToastHelper;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;
import java.util.Map;

public class AddContactsViewModel extends ContactsDetailsViewModel {

    public MutableLiveData<String> userName = new MutableLiveData<>();
    public MutableLiveData<String> sex = new MutableLiveData<>();
    public MutableLiveData<String> followStatus = new MutableLiveData<>();
    public MutableLiveData<String> contactFrom = new MutableLiveData<>();
    public MutableLiveData<String> weight = new MutableLiveData<>();
    public MutableLiveData<String> remark = new MutableLiveData<>();

    public MutableLiveData<String> imgData = new MutableLiveData<>();

    public MutableLiveData<String> deleteTagStr = new MutableLiveData<>();

    public MutableLiveData<String> addContactsSuccessData = new MutableLiveData<>();

    public MutableLiveData<List<ContactsTagModel>> historyTagsData = new MutableLiveData<>();

    /**
     * 新增联系人
     */
    public void saveContacts(Map<String, Object> map) {
        addSubscribe(mRetrofitHelper.saveContractInfo(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    addContactsSuccessData.setValue(baseModel.getMsg());
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshContactsList));
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }
        }));
    }

    /**
     * 编辑联系人
     */
    public void editContacts(Map<String, Object> map, String id) {
        map.put("id", id);
        addSubscribe(mRetrofitHelper.editContractInfo(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    addContactsSuccessData.setValue(baseModel.getMsg());
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshContactsList));
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshContactDetailsData));
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }
        }));
    }

    public void getTag() {
        addSubscribe(mRetrofitHelper.getAllHistoryTags(0).subscribeWith(new BaseNetCallback<List<ContactsTagModel>>() {
            @Override
            public void onSuccess(BaseModel<List<ContactsTagModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    historyTagsData.setValue(baseModel.getData());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                historyTagsData.setValue(null);
            }
        }));
    }

//    /**
//     * 删除标签
//     */
//    public void deleteTag(String tagId) {
//        addSubscribe(mRetrofitHelper.deleteTag(tagId).subscribeWith(new BaseNetCallback<String>() {
//            @Override
//            public void onSuccess(BaseModel<String> baseModel) {
//                if (Constant.SUCCESS == baseModel.getCode()) {
//                    deleteTagStr.setValue("删除成功");
//                    EventBus.getDefault().post(new EventBusModel(Constant.refreshContactsList));
//                }
//            }
//        }));
//    }

    /**
     * 删除标签
     */
    public void deleteAllTag(List<ContactsTagModel> data) {
        addSubscribe(mRetrofitHelper.deleteAllTag(new Gson().toJson(data)).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                deleteTagStr.setValue("删除");
                if (Constant.SUCCESS == baseModel.getCode()) {
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshContactsList));
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                deleteTagStr.setValue("删除");
            }
        }));
    }


    /**
     * 图片上传
     */
    public void uploadImage(File file) {
        addSubscribe(mRetrofitHelper.uploadImage(file).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    imgData.setValue(ApiFactory.IMAGE_UPLOAD + baseModel.getData());
                } else {
                    ToastHelper.show("上传失败");
                }
            }
        }));
    }

}
